package com.tracking.locon;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnKeyListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;

public class Chat extends Fragment implements OnClickListener {

	private ChatArrayAdapter mAdapter;
	private ListView mListView;
	private EditText mMessageBox;
	private Button mSendButton;

	private static int mMessageCount = 0;

	@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.chat, container, false);
        setupChat(view);
        return view;
    }
	
	private void setupChat(View view){
		// Set chat adapter in the list view
		mListView = (ListView) view.findViewById(R.id.lvChatRoom);
		mAdapter = new ChatArrayAdapter(view.getContext(), R.layout.chat);
		mListView.setAdapter(mAdapter);

		// Set click listener on the button
		mSendButton = (Button) view.findViewById(R.id.bSend);
		mSendButton.setOnClickListener(this);

		// Set keyboard listener on the editText
		mMessageBox = (EditText) view.findViewById(R.id.etChatbox);
		mMessageBox.setOnKeyListener(new OnKeyListener() {
			public boolean onKey(View v, int keyCode, KeyEvent event) {
				// If the event is a key-down event on the "enter" button
				if ((event.getAction() == KeyEvent.ACTION_DOWN)
						&& (keyCode == KeyEvent.KEYCODE_ENTER)) {
					// Perform action on key press
					onClick(mMessageBox);
					return true;
				}
				return false;
			}
		});

		mMessageBox.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				Utility.smoothScroll(mListView);
			}
		});
	}
	/*
	@Override
	protected void onCreateView(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.chat);

		// Set chat adapter in the list view
		mListView = (ListView) findViewById(R.id.lvChatRoom);
		mAdapter = new ChatArrayAdapter(getApplicationContext(), R.layout.chat);
		mListView.setAdapter(mAdapter);

		// Set click listener on the button
		mSendButton = (Button) findViewById(R.id.bSend);
		mSendButton.setOnClickListener(this);

		// Set keyboard listener on the edittext
		mMessageBox = (EditText) findViewById(R.id.etChatbox);
		mMessageBox.setOnKeyListener(new OnKeyListener() {
			public boolean onKey(View v, int keyCode, KeyEvent event) {
				// If the event is a key-down event on the "enter" button
				if ((event.getAction() == KeyEvent.ACTION_DOWN)
						&& (keyCode == KeyEvent.KEYCODE_ENTER)) {
					// Perform action on key press
					onClick(mMessageBox);
					return true;
				}
				return false;
			}
		});

		mMessageBox.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				Utility.smoothScroll(mListView);
			}
		});
	}
*/
	public void onClick(View arg0) {
		String message = mMessageBox.getText().toString().trim();
		// If message is not empty
		if (!message.equals("")) {
			Chat.mMessageCount++; // TODO delete this when the real chat is
									// finished
			// Get current time stamp
			long currentTime = System.currentTimeMillis();

			// If the count is even, bubble in right side
			if (Chat.mMessageCount % 2 == 0) {
				mAdapter.add(new ChatComment(false, message, currentTime));
			}
			// If the count is odd, bubble in left side
			else {
				mAdapter.add(new ChatComment(true, message, currentTime));
			}
			mMessageBox.setText("");

			// Scroll to the last message after typing
			Utility.smoothScroll(mListView);
			//sendMessage(message);
		}
	}
	/*
	private void sendMessage(String message) {
		Message msgToSend = new Message(mContact.getJIDWithRes(),
				Message.MSG_TYPE_CHAT);
		msgToSend.setBody(inputContent);

		try {
			if (mChat == null) {
				mChat = mChatManager.createChat(mContact, mMessageListener);
				mChat.setOpen(true);
			}
			mChat.sendMessage(msgToSend);
		} catch (RemoteException e) {
			Log.e(TAG, e.getMessage());
		}

		final String self = getString(R.string.chat_self);
		MessageText lastMessage = null;
		if (mListMessages.size() != 0)
			lastMessage = mListMessages.get(mListMessages.size() - 1);

		if (lastMessage != null && lastMessage.getName().equals(self)) {
			lastMessage.setMessage(lastMessage.getMessage().concat(
					"\n" + inputContent));
			lastMessage.setTimestamp(new Date());
		} else
			mListMessages.add(new MessageText(self, self, inputContent, false,
					new Date()));
		mMessagesListAdapter.notifyDataSetChanged();
		mInputField.setText(null);

	}
	*/
}